package com.example.tellwin.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestOptions;
import com.example.tellwin.BuildConfig;
import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.common.Common;

import java.security.MessageDigest;

/**
 * Created by Administrator on 2018/4/30.
 */

public class GlideUtils {

    /**
     * 加载网络图片
     * @param mContext
     * @param path
     * @param imageview
     */
    public static void LoadImage(Context mContext, String path,
                                 ImageView imageview) {
        RequestOptions options = new RequestOptions()
                .centerCrop().placeholder(R.mipmap.ic_launcher)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(mContext).load(path).apply(options).into(imageview);
    }

    /**
     * 加载网络图片
     * @param mContext
     * @param path
     * @param imageview
     */
    public static void LoadImage(Context mContext, String path,
                                 ImageView imageview,int errorResource) {
        RequestOptions options = new RequestOptions()
                .error(errorResource);
        Glide.with(mContext).load(path).apply(options).into(imageview);
    }

    /**
     * 加载网络图片
     * @param mContext
     * @param path
     * @param imageview
     */
    public static void LoadImage(Context mContext, Uri path,
                                 ImageView imageview) {
        RequestOptions options = new RequestOptions()
                .centerCrop().placeholder(R.mipmap.ic_launcher)
                .diskCacheStrategy(DiskCacheStrategy.NONE);
        Glide.with(mContext).load(path).apply(options).into(imageview);
    }

    /**
     * 加载网络图片
     * @param mContext
     * @param path
     * @param imageview
     */
    public static void loadHeadImage(Context mContext, String path,
                                     ImageView imageview) {
        int icon = Common.ANSWER.equals(MainApplication.Identity)?R.mipmap.teacher_icon:R.mipmap.student_icon;
        if (mContext != null){
            RequestOptions options = new RequestOptions()
                    .placeholder(icon)
                    .error(icon)
                    .optionalTransform
                            (new GlideRoundedCornersTransform(PixelUtil.dp2px(5)
                                    , GlideRoundedCornersTransform.CornerType.ALL))
                    .circleCrop();
            Glide.with(mContext).load(path).apply(options).into(imageview);
        }
    }

    /**
     * 加载网络图片
     * @param mContext
     * @param imageview
     */
    public static void loadHeadDefalutImage(Context mContext,
                                     ImageView imageview) {
        if (mContext != null){
            int icon = Common.ANSWER.equals(MainApplication.Identity)?R.mipmap.teacher_icon:R.mipmap.student_icon;
            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.student_icon)
                    .error(R.mipmap.student_icon)
                    .optionalTransform
                            (new GlideRoundedCornersTransform(PixelUtil.dp2px(5)
                                    , GlideRoundedCornersTransform.CornerType.ALL))
                    .circleCrop();
            Glide.with(mContext).load(icon).apply(options).into(imageview);
        }
    }

    /**
     * 加载网络图片
     * @param mContext
     * @param imageview
     */
    public static void loadHeadImage(Context mContext, int icon ,
                                            ImageView imageview) {
        if (mContext != null){
            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.student_icon)
                    .error(R.mipmap.student_icon)
                    .optionalTransform
                            (new GlideRoundedCornersTransform(PixelUtil.dp2px(5)
                                    , GlideRoundedCornersTransform.CornerType.ALL))
                    .circleCrop();
            Glide.with(mContext).load(icon).apply(options).into(imageview);
        }
    }


    /**
     * 加载带尺寸的图片
     * @param mContext
     * @param path
     * @param Width
     * @param Height
     * @param imageview
     */
    public static void LoadImageWithSize(Context mContext, String path,
                                         int Width, int Height, ImageView imageview) {
        RequestOptions options = new RequestOptions()
                .override(Width, Height)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(mContext).load(path).apply(options).into(imageview);
    }

    /**
     * 加载本地图片
     * @param mContext
     * @param path
     * @param imageview
     */
    public static void LoadImageWithLocation(Context mContext, Integer path,
                                             ImageView imageview) {
        RequestOptions options = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(mContext).load(path).apply(options)
                .into(imageview);
    }

    /**
     * 加载本地图片
     * @param mContext
     * @param path
     * @param imageview
     */
    public static void LoadCircleImageWithLocation(Context mContext, Integer path,
                                             ImageView imageview) {
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)
                .optionalTransform
                        (new GlideRoundedCornersTransform(PixelUtil.dp2px(5)
                                , GlideRoundedCornersTransform.CornerType.ALL))
                .circleCrop();
        Glide.with(mContext).load(path).apply(options).into(imageview);
    }

    /**
     * 圆形加载
     *
     * @param mContext
     * @param path
     * @param imageview
     */
    public static void LoadCircleImage(Context mContext, String path,
                                       ImageView imageview) {
        if (mContext != null){
            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.ic_launcher)
                    .optionalTransform
                            (new GlideRoundedCornersTransform(PixelUtil.dp2px(5)
                                    , GlideRoundedCornersTransform.CornerType.ALL))
                    .circleCrop();
            Glide.with(mContext).load(path).apply(options).into(imageview);
        }
    }

    /**
     * 圆形加载
     *
     * @param mContext
     * @param path
     * @param imageview
     */
    public static void LoadCircleImage(Context mContext, String path,
                                       ImageView imageview , int errorPicId) {
        if (mContext != null){
            RequestOptions options = new RequestOptions()
                    .placeholder(errorPicId)
                    .optionalTransform
                            (new GlideRoundedCornersTransform(PixelUtil.dp2px(5)
                                    , GlideRoundedCornersTransform.CornerType.ALL))
                    .circleCrop();
            Glide.with(mContext).load(path).apply(options).into(imageview);
        }
    }

    public static void loadRoundImage(Context context,String url,ImageView imageView,int dp){
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)
                .optionalTransform
                        (new GlideRoundImage(context,dp));
        Glide.with(context)
                .load(url)
                .apply(options)
                .into(imageView);
    }

    static class GlideRoundedCornersTransform extends CenterCrop {
        private float mRadius;
        private CornerType mCornerType;
        private static final int VERSION = 1;
        private static final String ID = BuildConfig.APPLICATION_ID + "GlideRoundedCornersTransform." + VERSION;
        private static final byte[] ID_BYTES = ID.getBytes(CHARSET);

        /** 待处理的圆角枚举*/
        public enum CornerType {
            ALL,
            TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT,
            TOP, BOTTOM, LEFT, RIGHT,
            TOP_LEFT_BOTTOM_RIGHT,
            TOP_RIGHT_BOTTOM_LEFT,
            TOP_LEFT_TOP_RIGHT_BOTTOM_RIGHT,
            TOP_RIGHT_BOTTOM_RIGHT_BOTTOM_LEFT,
            TOP_LEFT_TOP_RIGHT
        }

        public GlideRoundedCornersTransform(float radius, CornerType cornerType) {
            super();
            mRadius = radius;
            mCornerType = cornerType;
        }

        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            Bitmap transform = super.transform(pool, toTransform, outWidth, outHeight);
            return roundCrop(pool, transform);
        }

        private Bitmap roundCrop(BitmapPool pool, Bitmap source) {
            if (source == null) {
                return null;
            }
            int width = source.getWidth();
            int height = source.getHeight();
            Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);

            if (result == null) {
                result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config
                        .ARGB_8888);
            }
            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader
                    .TileMode.CLAMP));
            paint.setAntiAlias(true);

            Path path = new Path();
            drawRoundRect(canvas, paint, path, width, height);

            return result;
        }

        private void drawRoundRect(Canvas canvas, Paint paint, Path path, int width, int height) {
            float[] rids ;
            switch (mCornerType) {
                case ALL:
                    rids = new float[]{mRadius,mRadius,mRadius,mRadius,mRadius,mRadius,mRadius,mRadius};
                    drawPath(rids,canvas, paint, path, width, height);
                    break;
                case TOP_LEFT:
                    rids = new float[]{mRadius,mRadius,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f};
                    drawPath(rids,canvas, paint, path, width, height);
                    break;
                case TOP_RIGHT:
                    rids  = new float[]{0.0f,0.0f,mRadius,mRadius,0.0f,0.0f,0.0f,0.0f};
                    drawPath(rids,canvas, paint, path, width, height);
                    break;
                case BOTTOM_RIGHT:
                    rids  = new float[]{0.0f,0.0f,0.0f,0.0f,mRadius,mRadius,0.0f,0.0f};
                    drawPath(rids,canvas,  paint, path, width, height);
                    break;
                case BOTTOM_LEFT:
                    rids  = new float[]{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,mRadius,mRadius};
                    drawPath(rids,canvas,  paint, path, width, height);
                    break;
                case TOP:
                    rids = new float[]{mRadius,mRadius,mRadius,mRadius,0.0f,0.0f,0.0f,0.0f};
                    drawPath(rids,canvas,  paint,  path,width, height);
                    break;
                case BOTTOM:
                    rids  = new float[]{0.0f,0.0f,0.0f,0.0f,mRadius,mRadius,mRadius,mRadius};
                    drawPath(rids,canvas,  paint, path, width, height);
                    break;
                case LEFT:
                    rids = new float[]{mRadius,mRadius,0.0f,0.0f,0.0f,0.0f,mRadius,mRadius};
                    drawPath(rids,canvas,  paint, path, width, height);
                    break;
                case RIGHT:
                    rids  = new float[]{0.0f,0.0f,mRadius,mRadius,mRadius,mRadius,0.0f,0.0f};
                    drawPath(rids,canvas,  paint, path, width, height);
                    break;
                case TOP_LEFT_BOTTOM_RIGHT:
                    rids  = new float[]{mRadius,mRadius,0.0f,0.0f,mRadius,mRadius,0.0f,0.0f};
                    drawPath(rids,canvas,  paint, path, width, height);
                    break;
                case TOP_RIGHT_BOTTOM_LEFT:
                    rids  = new float[]{0.0f,0.0f,mRadius,mRadius,0.0f,0.0f,mRadius,mRadius};
                    drawPath(rids,canvas,  paint, path, width, height);
                    break;
                case TOP_LEFT_TOP_RIGHT_BOTTOM_RIGHT:
                    rids  = new float[]{mRadius,mRadius,mRadius,mRadius,mRadius,mRadius,0.0f,0.0f};
                    drawPath(rids,canvas,  paint, path, width, height);
                    break;
                case TOP_RIGHT_BOTTOM_RIGHT_BOTTOM_LEFT:
                    rids  = new float[]{0.0f,0.0f,mRadius,mRadius,mRadius,mRadius,mRadius,mRadius};
                    drawPath(rids,canvas,  paint,  path,width, height);
                    break;
                case TOP_LEFT_TOP_RIGHT:
                    rids = new float[]{mRadius,mRadius,mRadius,mRadius,0.0f,0.0f,0.0f,0.0f};
                    drawPath(rids,canvas, paint, path, width, height);
                    break;
                default:
                    throw new RuntimeException("RoundedCorners type not belong to CornerType");
            }
        }

        /**@param rids 圆角的半径，依次为左上角xy半径，右上角，右下角，左下角*/
        private void drawPath(float[] rids, Canvas canvas, Paint paint, Path path, int width, int height) {
            path.addRoundRect(new RectF(0, 0, width, height), rids, Path.Direction.CW);
            canvas.drawPath(path,paint);
        }

        @Override
        public boolean equals(Object o) {
            return o instanceof GlideRoundedCornersTransform;
        }

        @Override
        public int hashCode() {
            return ID.hashCode();
        }

        @Override
        public void updateDiskCacheKey(MessageDigest messageDigest) {
            messageDigest.update(ID_BYTES);
        }
    }
}
